/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package com.google.code.twiddling.core.io;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import jline.ConsoleReader;
import jline.UnixTerminal;
import junit.framework.TestCase;

/**
 * @author <a href="mailto:jeff.yuchang@gmail.com">Jeff Yu</a>
 *
 */
public abstract class JLineTestCase extends TestCase {
    ConsoleReader console;

    public JLineTestCase(String test) {
        super(test);
    }

    public void setUp() throws Exception {
        super.setUp();
        console = new ConsoleReader(null, new PrintWriter(
                new OutputStreamWriter(new ByteArrayOutputStream())), null,
                new UnixTerminal());
    }

    public void assertBuffer(String expected, Buffer buffer) throws IOException {
        assertBuffer(expected, buffer, true);
    }

    public void assertBuffer(String expected, Buffer buffer, boolean clear)
            throws IOException {
        // clear current buffer, if any
        if (clear) {
            //console.finishBuffer();
            console.getHistory().clear();
        }

        console.setInput(new ByteArrayInputStream(buffer.getBytes()));

        // run it through the reader
        while (console.readLine((String) null) != null) {
            ;
        }

        assertEquals(expected, console.getCursorBuffer().toString());
    }

    private int getKeyForAction(short logicalAction) {
        
        if (logicalAction == -1) {
            fail("Keystroke for logical action " + logicalAction
                    + " was not bound in the console");
        }

        return logicalAction;
    }

    /**
     * TODO: Fix this so tests don't break on windows machines.
     *
     * @author Ryan
     *
     */
    class Buffer {
        private final ByteArrayOutputStream bout = new ByteArrayOutputStream();

        public Buffer() {
        }

        public Buffer(String str) {
            append(str);
        }

        public byte[] getBytes() {
            return bout.toByteArray();
        }

        public Buffer op(short operation) {
            return append(getKeyForAction(operation));
        }

        public Buffer ctrlA() {
            return append(getKeyForAction(ConsoleReader.MOVE_TO_BEG));
        }

        public Buffer ctrlU() {
            return append(getKeyForAction(ConsoleReader.KILL_LINE_PREV));
        }

        public Buffer tab() {
            return append(getKeyForAction(ConsoleReader.COMPLETE));
        }

        public Buffer back() {
            return append(getKeyForAction(ConsoleReader.DELETE_PREV_CHAR));
        }

        public Buffer left() {
            return append(UnixTerminal.ARROW_START).append(
                    UnixTerminal.ARROW_PREFIX).append(UnixTerminal.ARROW_LEFT);
        }

        public Buffer right() {
            return append(UnixTerminal.ARROW_START).append(
                    UnixTerminal.ARROW_PREFIX).append(UnixTerminal.ARROW_RIGHT);
        }

        public Buffer up() {
            return append(UnixTerminal.ARROW_START).append(
                    UnixTerminal.ARROW_PREFIX).append(UnixTerminal.ARROW_UP);
        }

        public Buffer down() {
            return append(UnixTerminal.ARROW_START).append(
                    UnixTerminal.ARROW_PREFIX).append(UnixTerminal.ARROW_DOWN);
        }

        public Buffer append(String str) {
            byte[] bytes = str.getBytes();

            for (int i = 0; i < bytes.length; i++) {
                append(bytes[i]);
            }

            return this;
        }

        public Buffer append(int i) {
            return append((byte) i);
        }

        public Buffer append(byte b) {
            bout.write(b);

            return this;
        }
    }
}
